package com.ctshk.sapp.order.custom.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.rpc.order.custom.dto.order.*;
import com.ctshk.rpc.order.custom.req.order.*;
import com.ctshk.rpc.order.custom.service.ICustomizedOrderReceiptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import com.ctshk.common.web.support.BaseController;

import javax.validation.Valid;

/**
 * <p>
 * 订单收据 前端控制器
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-02-22
 */
@Api(value="收據", tags="收據")
@RestController
@Slf4j
@RequestMapping("/customized-order-receipt")
public class CustomizedOrderReceiptController extends BaseController {

    @DubboReference
    private ICustomizedOrderReceiptService orderReceiptService;


    @ApiOperation(value = "列表", notes = "收據列表（收據管理）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "收據列表", operDesc = "收據列表")
    @PostMapping("/list")
    public Result<CustomOrderReceiptListCountDTO> list(@RequestBody @Valid OrderReceiptListReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return Result.success(orderReceiptService.list(req));
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "打印收據-旅客列表", notes = "打印收據-旅客列表（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "收據管理-打印收據", operDesc = "收據管理-打印收據")
    @PostMapping("/list-print")
    public Result<CustomOrderReceiptListTouristDTO> listPrint(@RequestBody @Valid CustomOrderReceiptListTouristReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return Result.success(orderReceiptService.listPrint(req));
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "销售收据预览", notes = "销售收据预览（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "收據管理-打印收據-打印预览", operDesc = "收據管理-打印收據-打印预览")
    @PostMapping("/preview")
    public Result<CustomOrderReceiptDetailsPreviewDTO> preview(@RequestBody @Valid CustomOrderReceiptDetailsPreviewReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderReceiptService.preview(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "查看收據", notes = "查看收據（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "收據管理-查看", operDesc = "收據管理-查看")
    @PostMapping("/detail")
    public Result<CustomOrderReceiptInfoDTO> detail(@RequestBody @Valid CustomOrderReceiptDetailsIdReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderReceiptService.detail(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "重印收據", notes = "重印收據（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "收據管理-重印", operDesc = "收據管理-重印")
    @PostMapping("/re-print")
    public Result<CustomOrderReceiptDetailsPreviewDTO> rePrint(@RequestBody @Valid CustomOrderReceiptDetailsRePrintReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderReceiptService.rePrint(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "收據作废", notes = "收據作废（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "收據管理-作廢", operDesc = "收據管理-作廢")
    @PostMapping("/invalid")
    public Result add(@RequestBody @Valid CustomOrderReceiptInvalidReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderReceiptService.invalid(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "打印", notes = "销售收据预览-打印（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "銷售收據-打印", operDesc = "銷售收據-打印")
    @PostMapping("/print-receipt")
    public Result printReceipt(@RequestBody @Valid CustomOrderReceiptDetailsRePrintReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderReceiptService.printReceipt(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "打印整單收據", notes = "收據管理-打印整單收據（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "收據管理-打印整單收據", operDesc = "收據管理-打印整單收據")
    @PostMapping("/print-all")
    public Result printAll(@RequestBody @Valid OrderReceiptPrintAllReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderReceiptService.invalidAll(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "批量打印", notes = "批量打印（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "銷售收據-批量打印", operDesc = "銷售收據-批量打印")
    @PostMapping("/print-batch")
    public Result printBatch(@RequestBody @Valid CustomOrderReceiptPrintBatchReq req) {
        try {
            req.setUserId(getCurrentUser(getRequest()).getId());
            return orderReceiptService.printBatch(req);
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

    @ApiOperation(value = "取消", notes = "销售收据预览-取消（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "銷售收據-取消", operDesc = "銷售收據-取消")
    @GetMapping("/cancel/{id}")
    public Result cancel(@PathVariable Long id) {
        try {
            return orderReceiptService.cancel(id,getCurrentUser(getRequest()).getId());
        }catch (BusinessException e){
            return Result.failed(e);
        }
    }

}
